home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / sep91.zip / 9N09122A < prev    next >
Text File  |  1991-05-13  |  4KB  |  156 lines

  1. /* Listing 1 */
  2.  
  3. #include <graphics.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <conio.h>
  7. #include <math.h>
  8. #include <time.h>       /* for use by randomize */
  9.  
  10. #define MAX_CIRCLES 150
  11.  
  12. double distance( double x, double y, int i);
  13. void generate_circles();
  14. void exit_if_kbhit();
  15.  
  16. typedef struct {
  17.        int x;
  18.        int y;
  19.        int r;
  20.        } circle_type;
  21.  
  22. circle_type c[ MAX_CIRCLES];
  23.  
  24. int N, rmax;
  25.  
  26.  
  27. /************************************************/
  28.  
  29. void generate_circles()
  30.  
  31. /* this function draws a series of circles at random */
  32. /* positions, each one after the first touching the  */
  33. /* nearest circle that has been previously drawn     */
  34.  
  35. {
  36.    int xmax, ymax, x_pos, y_pos, radius;
  37.    randomize();      /* initialize the random numbers */
  38.    xmax = getmaxx(); /* maximum x and y positions  */
  39.    ymax = getmaxy(); /* that can be displayed      */
  40.  
  41.    rmax = max( xmax, ymax)/2;
  42.           /* divisor of 2 can be changed to allow */
  43.           /* different maximum radii              */
  44.  
  45.    /* select random position and draw first circle */
  46.  
  47.    x_pos = rand() % xmax;
  48.    y_pos = rand() % ymax;
  49.    radius = rand() % rmax;
  50.    c[0].x = x_pos;
  51.    c[0].y = y_pos;
  52.    c[0].r = radius;
  53.    circle( x_pos, y_pos, radius);
  54.  
  55.    /* select and draw remaining randomly placed circles */
  56.    /* each tangent to the nearest previously drawn      */
  57.    /* circle                                            */
  58.  
  59.    for ( N = 1; N <= MAX_CIRCLES; N++)
  60.        {
  61.        do
  62.            {
  63.            exit_if_kbhit();
  64.            x_pos = rand() % xmax;
  65.            y_pos = rand() % ymax;
  66.            radius = new_radius( x_pos, y_pos);
  67.            } while (radius <= 0);
  68.        radius = min( rmax, radius);
  69.        circle( x_pos, y_pos, radius);
  70.        c[N].x = x_pos;
  71.        c[N].y = y_pos;
  72.        c[N].r = radius;
  73.        }
  74. }
  75.  
  76. /************************************************/
  77.  
  78. int new_radius( int x, int y)
  79.  
  80. /* returns the distance from the point x,y to the   */
  81. /* nearest circle; aborts and returns the           */
  82. /* calculated negative or zero distance if x,y lies */
  83. /* on or inside another circle                      */
  84.  
  85. {
  86.     int i, radius;
  87.     radius = rmax;
  88.     for ( i = 0; i <= N; i++)
  89.         {
  90.         radius = min( radius, (int)distance(x,y,i));
  91.         if ( radius <= 0) return radius;
  92.         }
  93.     return radius;
  94. }
  95.  
  96. /************************************************/
  97.  
  98. double distance( double x, double y, int i)
  99.  
  100. /* returns the distance from the point x,y to    */
  101. /* circle i; returns a negative or zero distance */
  102. /* if x,y is inside or on circle i               */
  103.  
  104. {
  105.     double xdist, ydist, distancesq;
  106.     xdist = x - c[i].x;
  107.     ydist = y - c[i].y;
  108.     distancesq = xdist*xdist + ydist*ydist;
  109.     return ( sqrt( distancesq) - c[i].r);
  110. }
  111.  
  112. /************************************************/
  113.  
  114. int main(void)
  115.  
  116. /* initializes Borland Graphics Interface in      */
  117. /* "audodetect" mode using default background     */
  118. /* and foreground colors, checks for errors in    */
  119. /* initialization, calls routine to draw circles, */
  120. /* when finished waits for keystroke to erase     */
  121. /* screen and exit                                */
  122.  
  123. {
  124.    int gdriver = DETECT, gmode, errorcode;
  125.    initgraph(&gdriver, &gmode, "");
  126.    errorcode = graphresult();
  127.    if (errorcode != grOk)  /* an error occurred */
  128.       {
  129.       printf("Graphics error: %s\n",
  130.                        grapherrormsg(errorcode));
  131.       printf("Press any key to halt:");
  132.       getch();
  133.       exit(1);
  134.       }
  135.    generate_circles();
  136.    getch();
  137.    closegraph();
  138.    return 0;
  139. }
  140.  
  141. /************************************************/
  142.  
  143. void exit_if_kbhit()
  144.  
  145. /* exits program if a key has been struck */
  146. /* returns otherwise                      */
  147.  
  148. {
  149.      if ( kbhit())
  150.         {
  151.         getch();
  152.         closegraph();
  153.         exit(1);
  154.         }
  155. }
  156.